package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendPeriodStatDAO;
import org.jsola.hr.dto.AttendPeriodStatAddDTO;
import org.jsola.hr.dto.AttendPeriodStatUpdateDTO;
import org.jsola.hr.entity.AttendPeriodStatDO;
import org.jsola.hr.query.AttendPeriodStatQuery;
import org.jsola.hr.service.IAttendPeriodStatService;
import org.jsola.hr.vo.AttendPeriodStatListVO;
import org.jsola.hr.vo.AttendPeriodStatVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendPeriodStatServiceImpl")
public class AttendPeriodStatServiceImpl implements IAttendPeriodStatService {

    @Autowired
    private IAttendPeriodStatDAO attendPeriodStatDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendPeriodStatVO save(AttendPeriodStatAddDTO attendPeriodStatAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendPeriodStatDO attendPeriodStatDO = attendPeriodStatAddDTO.to(AttendPeriodStatDO.class);
        // 保存
        attendPeriodStatDO = save(attendPeriodStatDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendPeriodStatDO.to(AttendPeriodStatVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendPeriodStatUpdateDTO attendPeriodStatUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendPeriodStatDO attendPeriodStatDO = attendPeriodStatUpdateDTO.to(AttendPeriodStatDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendPeriodStatDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendPeriodStatIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendPeriodStatIds);
    }


    @Override
    public AttendPeriodStatVO selectById(String attendPeriodStatId, String siteId) {
        AttendPeriodStatDO attendPeriodStatDO = selectDOById(attendPeriodStatId, siteId);
        if (attendPeriodStatDO == null) {
            return null;
        }
        return attendPeriodStatDO.to(AttendPeriodStatVO.class);
    }

    @Override
    public List<AttendPeriodStatListVO> select(AttendPeriodStatQuery attendPeriodStatQuery, String siteId) {
        List<AttendPeriodStatDO> attendPeriodStatDOList = selectDO(attendPeriodStatQuery, siteId);
        if(CollectionUtils.isEmpty(attendPeriodStatDOList)) {
            return attendPeriodStatDOList == null ? null : new ArrayList<>();
        }
        return attendPeriodStatDOList.stream()
                .map(attendPeriodStatDO -> attendPeriodStatDO.to(AttendPeriodStatListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendPeriodStatQuery attendPeriodStatQuery, String siteId) {
        Example example = buildExample(attendPeriodStatQuery,siteId);
        return attendPeriodStatDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendPeriodStatListVO> selectPage(AttendPeriodStatQuery attendPeriodStatQuery, String siteId) {
        Example example = buildExample(attendPeriodStatQuery,siteId);
        Page<AttendPeriodStatDO> page = attendPeriodStatDAO.selectPageByExample(example,
                attendPeriodStatQuery.getPageNo(),
                attendPeriodStatQuery.getPageSize());

        return page.to(AttendPeriodStatListVO.class);
    }

    @Override
    public AttendPeriodStatDO selectDOById(String attendPeriodStatId, String siteId) {
        return listById(attendPeriodStatId, siteId);
    }

    @Override
    public List<AttendPeriodStatDO> selectDO(AttendPeriodStatQuery attendPeriodStatQuery, String siteId) {
        Example example = buildExample(attendPeriodStatQuery,siteId);
        return attendPeriodStatDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendPeriodStatQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendPeriodStatQuery attendPeriodStatQuery, String siteId) {
        Example example = new Example(AttendPeriodStatDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendPeriodStatQuery.getAttendRecordIds() != null && attendPeriodStatQuery.getAttendRecordIds().size() > 0) {
            example.and().andIn("attendRecordId", attendPeriodStatQuery.getAttendRecordIds());
        }
        if (attendPeriodStatQuery.getPeopleMonthlyStatId() != null) {
            example.and().andEqualTo("peopleMonthlyStatId", attendPeriodStatQuery.getPeopleMonthlyStatId());
        }
        if (attendPeriodStatQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendPeriodStatQuery.getCompanyId());
        }
        if (attendPeriodStatQuery.getAttendPlanId() != null) {
            example.and().andEqualTo("attendPlanId", attendPeriodStatQuery.getAttendPlanId());
        }
        if (attendPeriodStatQuery.getEmpInfoId() != null) {
            example.and().andEqualTo("empInfoId", attendPeriodStatQuery.getEmpInfoId());
        }
        if (attendPeriodStatQuery.getAttendPunchTimeId() != null) {
            example.and().andEqualTo("attendPunchTimeId", attendPeriodStatQuery.getAttendPunchTimeId());
        }
        if (attendPeriodStatQuery.getPeriod() != null) {
            example.and().andEqualTo("period", attendPeriodStatQuery.getPeriod());
        }
        if (attendPeriodStatQuery.getYearly() != null) {
            example.and().andEqualTo("yearly", attendPeriodStatQuery.getYearly());
        }
        if (attendPeriodStatQuery.getMonthly() != null) {
            example.and().andEqualTo("monthly", attendPeriodStatQuery.getMonthly());
        }
        if (attendPeriodStatQuery.getAttendRecordId() != null) {
            example.and().andEqualTo("attendRecordId", attendPeriodStatQuery.getAttendRecordId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendPeriodStatQuery.getOrders());
        return example;
    }
}




